import {mapMutations} from 'vuex'

export default {
    ...mapMutations([
        'upToken',
		'upUserInfo'
    ]),


	getCode() {
		let that = this
		if (that.loading) {
			that.$toast.msg('请求中...', {
				icon: 1
			})
			return false
		}
		that.loading = true
		that.$httpRequest.request({
			that: that,
			url: 'api/login/sendMobileCode',
			params: {
				mobile: that.formData.phone
			},
			success: res => {
				that.loading = false
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.loopNum()
				}
				that.$toast.msg(res.message, {
					icon: 1
				})
			}
		})
	},

    submitForm() {
        let that = this
        if (that.loading) {
            that.$toast.msg('请求中...', {
                icon: 1
            })
            return false
        }
        that.loading = true
			that.$httpRequest.request({
			    that: that,
			    url: 'Publics/login',
			    params: {
			        mobile: that.formData.mobile,
			        code: that.formData.code
			    },
			    success: res => {
			        that.loading = false
			        if (res.code === that.$httpConfig.SUCCESS_CODE) {
			            that.upToken(res.data.token)
                        let url = ''
                        url = '/pages/index/index'
			            uni.switchTab({
			                url
			            })
			        } else {
			            that.$toast.alert({
			                content: res.message,
			                callback: result => {

			                }
			            })
			        }
			    }
			})

    },
	loopNum() {
		this.intervalObj = setInterval(() => {
			this.num--
			if (this.num === 0) {
				clearInterval(this.intervalObj)
				this.num = 60
			}
		}, 1000)
	},

}
